package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Tag;
import com.czz.manage.query.TagQuery;
import com.czz.manage.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: TagAdminController
 * @Author Czz
 * @Date 2020/2/18
 * @Time 18:36
 **/
@RestController
@RequestMapping(value = "/admin/tags")
public class TagAdminController {

    @Autowired
    TagService tagService;

    /**
    * @Description 获取标签分页列表
    * @Param [page, limit, tagQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 18:39
    **/
    @GetMapping(value = "{page}/{limit}")
    public R getTagPageList(@PathVariable Long page, @PathVariable Long limit, TagQuery tagQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }

        Page<Tag> pageParam = new Page<>(page, limit);
        tagService.getTagPageList(pageParam,tagQuery);
        List<Tag> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 新增问答标签
    * @Param [tag]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 18:41
    **/
    @PostMapping(value = "")
    public R saveTag(Tag tag){
        if (StringUtils.isEmpty(tag.getName())){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        tagService.save(tag);
        return R.ok();
    }

    /**
    * @Description 修改问答标签
    * @Param [id, tag]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 18:48
    **/
    @PutMapping(value = "{id}")
    public R updateTagById(@PathVariable String id,Tag tag){
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(tag.getName())){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        tag.setId(id);
        tagService.updateById(tag);
        return R.ok();
    }

    /**
    * @Description 根据id删除问答标签
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 19:53
    **/
    @DeleteMapping(value = "{id}")
    public R deleteTagById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Boolean flag = tagService.deleteTagById(id);
        if (flag) return R.ok();
        return R.setResult(ResultCodeEnum.INPUT_ERROR).message("删除失败！该标签还有问答使用中！");
    }

    /**
    * @Description 根据id查询问答标签
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/18
    * @Time 20:12
    **/
    @GetMapping(value = "{id}")
    public R getTagById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Tag tag = tagService.getById(id);
        return R.ok().data("item",tag);
    }

    /**
    * @Description 获取所有问答标签
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/4/27
    * @Time 19:35
    **/
    @GetMapping(value = "/list")
    public R getAllTagList(){
        List<Tag> tagList = tagService.getAllTagList();
        return R.ok().data("items",tagList);
    }
}
